package com.gl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gl.exception.BusinessException;
import com.gl.mapper.MeetingMapper;
import com.gl.mapper.UserMapper;
import com.gl.pojo.dto.MeetingPageDto;
import com.gl.pojo.entity.Meeting;
import com.gl.pojo.vo.AdminMeetingPageVo;
import com.gl.pojo.vo.MeetingPageVo;
import com.gl.pojo.vo.MeetingVo;
import com.gl.service.MeetingService;
import com.gl.utils.CurrentHolder;
import com.gl.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    private MeetingMapper meetingMapper;

    @Autowired
    private UserMapper userMapper;

    // 创建会议
    @Override
    public String createMeeting(Meeting meeting) {
        String meetingId = UuidUtils.getUuid();
        meeting.setId(meetingId);
        meeting.setStartTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        if (meeting.getTitle() == null || meeting.getTitle().isEmpty()) {
            meeting.setTitle("新会议");
        }
        String userId = CurrentHolder.getCurrentId();
        if (userId == null) {
            throw new BusinessException("用户不存在");
        }
        meeting.setUserId(userId);

        try {
            int result = meetingMapper.insertMeeting(meeting);
            return result > 0 ? meetingId : null;
        } catch (Exception e) {
            // Todo 处理数据库异常，建议记录日志
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateMeeting(Meeting meeting) {
        if (meeting.getId() != null) {
            return meetingMapper.updateMeeting(meeting);
        }
        return 0;
    }

    @Override
    public Meeting getMeetingInfo(String meetingId) {
        if (meetingId == null) {
            return null;
        }
        return meetingMapper.selectById(meetingId);
    }

    /**
     * 保存会议录制
     * @param meetingId 会议ID
     * @param recording 总结文本
     * @return -
     */
    @Override
    public int saveRecording(String meetingId, String recording) {
        if (meetingId != null && recording != null) {
            Meeting meeting = new Meeting();
            meeting.setId(meetingId);
            meeting.setRecording(recording);
            return meetingMapper.updateMeeting(meeting);
        }
        return 0;
    }

    /**
     * 分页查询会议
     * @param meetingPageDto 会议页信息
     * @return -
     */
    @Override
    public MeetingPageVo page(MeetingPageDto meetingPageDto) {
        if (meetingPageDto == null) meetingPageDto = new MeetingPageDto();
        meetingPageDto.setUserId(CurrentHolder.getCurrentId());
        Integer page = meetingPageDto.getPage();
        Integer pageSize = meetingPageDto.getPageSize();
        page = page == null ? 1 : page;
        pageSize = pageSize == null ? 10 : pageSize;

        PageHelper.startPage(page, pageSize);
        Page<Meeting> result = (Page<Meeting>) meetingMapper.pageQuery(meetingPageDto);
        //获取分页结果
        List<Meeting> meetingList = result.getResult();
        long total = result.getTotal();
        //封装
        MeetingPageVo meetingPageVo = new MeetingPageVo();
        meetingPageVo.setMeetingList(meetingList);
        meetingPageVo.setTotal(total);

        return meetingPageVo;
    }

    /**
     * 根据id删除会议
     *
     * @param meetingId 会议ID
     * @return -
     */
    @Override
    public int delete(String meetingId) {
        if (meetingId == null) {
            return 0;
        }
        return meetingMapper.deleteById(meetingId);
    }

    @Override
    public AdminMeetingPageVo pageAdmin(MeetingPageDto meetingPageDto) {
        if (meetingPageDto == null) meetingPageDto = new MeetingPageDto();
        Integer page = meetingPageDto.getPage();
        Integer pageSize = meetingPageDto.getPageSize();
        page = page == null ? 1 : page;
        pageSize = pageSize == null ? 10 : pageSize;

        PageHelper.startPage(page, pageSize);
        Page<MeetingVo> result = (Page<MeetingVo>) meetingMapper.adminPageQuery(meetingPageDto);
        //获取分页结果
        List<MeetingVo> meetingList = result.getResult();
        long total = result.getTotal();
        //封装
        AdminMeetingPageVo adminMeetingPageVo = new AdminMeetingPageVo();
        adminMeetingPageVo.setMeetingList(meetingList);
        adminMeetingPageVo.setTotal(total);

        return adminMeetingPageVo;
    }


}
